package cn.audit.repository.operation;

import cn.audit.bean.dto.UserCorrelation;
import cn.audit.bean.dto.zuzhangzhengshuai;
import cn.audit.bean.pojo.AuditPlan;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @Author: 钟琪
 * @Date: 2019/5/28 15:24
 */
public interface AuditPlanRepository extends JpaRepository<AuditPlan,Long>,JpaSpecificationExecutor<AuditPlan> {

    @Query("select new cn.audit.bean.dto.zuzhangzhengshuai(a,u) " +
            "from AuditPlan a ,User u " +
            "where a.creationname=u.id ")
    public List<zuzhangzhengshuai> bbb();

    @Query("select new cn.audit.bean.dto.zuzhangzhengshuai(a,u)" +
            "from AuditPlan a,User u " +
            "where a.id=?1 AND a.creationname=u.id ")
    public zuzhangzhengshuai findId(Long id);

    /**
     * 关联查询审计计划
     */
    @Query("select new cn.audit.bean.dto.UserCorrelation(a,i) " +
            "from AuditPlan a, AuditInstitution i " +
            "where a.institutions = i.id ")
    List<UserCorrelation> findPlanItemAll();

    /**
     * 查询审核中的数据
     *
     * @return
     */
    @Query("select new cn.audit.bean.dto.UserCorrelation(u,a) " +
            "from User u,AuditPlan a " +
            "where a.creationname = u.id " +
            "and a.status=2")
    List<UserCorrelation> findPlanStatus();

    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    cn.audit.bean.pojo.AuditPlan findAuditPlanById(Long id);

    @Modifying(clearAutomatically = true)
    @Query(value = "update AuditPlan a set a.status = ?1 where a.id = ?2")
    void updateAuditPlanStatus(Long status, Long id);

}
